package cn.bounce;

import java.awt.*;
import java.util.*;
import javax.swing.*;

/**
 * The component that draws the balls.
 * <p>
 * ©version 1.34 2012 -01 - 26
 * <p>
 * author Cay Horstmann
 * n V
 */
public class BallComponent extends JPanel {
    private static final int DEFAULT_WIDTH = 450;
    private static final int DEFAULT_HEICHT = 350;

    private java.util.List<Ball> balls = new ArrayList<>();


    public void add(Ball b) {
        balls.add(b);
    }

    public void paintComponent(Graphics g) {
        Random random = new Random(47);
        super.paintComponent(g); // erase background

        Graphics2D g2 = (Graphics2D) g;
        for (Ball b : balls) {
            g.setColor(new Color(random.nextInt(255),
                    random.nextInt(255),
                    random.nextInt(255)));
            g2.fill(b.getShape());
        }
    }

    public Dimension getPreferredSize() {
        return new Dimension(DEFAULT_WIDTH, DEFAULT_HEICHT);
    }
}
